package test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.math3.stat.inference.TTest;


/**
 * Created by lenovo on 2017/4/18.
 */
public class Teset {

    public static void main(String[] args) {

//        LengthConfig lengthConfig = new LengthConfig("", new int[0]);
        String qqqq = "{\"length\":[3,2,2],\"align\":\"left\"}";
        JSONObject jsonObject3 = JSON.parseObject(qqqq);
        if (null != jsonObject3) {
            String align = jsonObject3.containsKey("align") ? jsonObject3.getString("align") : "";
            JSONArray jsonArray1 = jsonObject3.containsKey("length") ? jsonObject3.getJSONArray("length") : new JSONArray();
            int[] intarray = new int[jsonArray1.size()];
            int i = 0;
            for (Object o : jsonArray1.toArray()) {
                intarray[i] = Integer.parseInt(o.toString());
//                i++;
                System.out.println(intarray[i]);
            }
//            System.out.println(intarray.toString());
//            lengthConfig = new LengthConfig(align, intarray);
        }


        double[] a = {1.3, 1.5, 1.5, 0.7, 1.1, 1.0, 0.8, 1.9, 1.7, 0.2};
        double[] b = {1.2, 1.9, 1.0, 0.3, 1.6, 1.4, 0.9, 1.1, 1.1, 0.5};
        System.out.println(new TTest().pairedT(a, b));
        ;
        System.out.println(new TTest().tTest(a, b));


        int[] nums = {1, 5, 7, 0, 0, 5, 1, 9, 7};
        System.out.println(new Teset().singleNumber(nums));

        String s = "the sky is blue";
        System.out.println(new Teset().reverseWords(s));
        System.out.println(new Teset().reverseWords2(s));

        TreeNode TreeNode0 = new TreeNode(0);
        TreeNode TreeNode2 = new TreeNode(2);
        TreeNode TreeNode3 = new TreeNode(3);
        TreeNode TreeNode4 = new TreeNode(4);
        TreeNode TreeNode5 = new TreeNode(5);
        TreeNode TreeNode6 = new TreeNode(6);
        TreeNode TreeNode7 = new TreeNode(7);
        TreeNode TreeNode8 = new TreeNode(8);
        TreeNode TreeNode9 = new TreeNode(9);

        TreeNode6.setLeft(TreeNode2);
        TreeNode6.setRight(TreeNode8);
        TreeNode2.setLeft(TreeNode0);
        TreeNode2.setRight(TreeNode4);
        TreeNode8.setLeft(TreeNode7);
        TreeNode8.setRight(TreeNode9);
        System.out.println(new Teset().lowestCommonAncestor(TreeNode6,TreeNode4,TreeNode7).val);
    }

    public int singleNumber(int[] nums) {
//        int[] init = new int[nums.length];
        int num = 0;

        for (int i = 0; i < nums.length; i++) {
            num ^= nums[i];
        }


        return num;
    }


    public String reverseWords(String s) {
        String[] sArrray = s.split(" ");
        String[] result = new String[sArrray.length];
        for (int i = 0; i < sArrray.length; i++) {
            result[sArrray.length - i-1] = sArrray[i];
        }
        StringBuffer re = new StringBuffer(32);
        for(String ss :result){
            re.append(ss);
            re.append(" ");
        }

        return re.toString();
    }


    public String reverseWords2(String s) {
        int storeIndex = 0, n = s.length();
        StringBuilder sb = new StringBuilder(s).reverse();
        for (int i = 0; i < n; ++i) {
            if (sb.charAt(i) != ' ') {
                if (storeIndex != 0) sb.setCharAt(storeIndex++, ' ');
                int j = i;
                while (j < n && sb.charAt(j) != ' ') sb.setCharAt(storeIndex++, sb.charAt(j++));
                String t = new StringBuilder(sb.substring(storeIndex - (j - i), storeIndex)).reverse().toString();
                sb.replace(storeIndex - (j - i), storeIndex, t);
                i = j;
            }
        }
        sb.setLength(storeIndex);
        return sb.toString();
    }



        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if(root==null || p==null || q==null) return null;

            if(Math.max(p.val, q.val) < root.val) {
                return lowestCommonAncestor(root.left, p, q);
            } else if(Math.min(p.val, q.val) > root.val) {
                return lowestCommonAncestor(root.right, p, q);
            } else return root;
        }

}
